package cn.edu.scau.cmi.lintianwei.hibernate.clientController;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.lintianwei.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.lintianwei.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.lintianwei.hibernate.domain.Student;
import cn.edu.scau.cmi.lintianwei.hibernate.domain.Teacher;
import javafx.event.ActionEvent;

public class StudentManageController {
	@FXML
	private Label flabel;
	@FXML
	private Label slabel;
	@FXML
	private Label tlabel;
	@FXML
	private TextField first;
	@FXML
	private TextField second;
	@FXML
	private TextField third;
	@FXML
	private Button start;
	@FXML
	private Button addS;
	@FXML
	private Button addT;
	@FXML
	private Button updateS;
	@FXML
	private Button updateT;
	@FXML
	private Button deleteS;
	@FXML
	private Button deleteT;
	@FXML
	private Button choseIT;
	@FXML
	private Button updateIT;
	@FXML
	private Button choseIS;
	@FXML
	private Button updateIS;
	@FXML
	private TextArea show;
	@FXML
	private Button listAllS;
	@FXML
	private Button listAllT;

	private String operation = null;
	private boolean isStudent = true;

	// Event Listener on Button.onAction
	@FXML
	public void btnEvent(ActionEvent event) {
		// 寰楀埌鎸夐挳瀹炰緥
		Button btn = (Button) event.getSource();
		if (btn != start && btn != listAllS && btn != listAllT) {
			first.clear();
			second.clear();
			third.clear();
		}
		if (btn == start) {
			first.setEditable(false);
			second.setEditable(false);
			third.setEditable(false);
			if (first.getText().length() <= 0)
				return;
			if (isStudent) {
				studentOperation(operation, null);
				listStudent();
			} else {
				teacherOperation(operation, null);
				listTeacher();
			}
			isStudent = true;
			operation = null;
		} else if (btn == addS) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("瀛︾敓ID锛�");
			slabel.setText("瀛︾敓濮撳悕锛�");
			tlabel.setText("鑰佸笀ID锛�");
			operation = btn.getText();
			isStudent = true;
		} else if (btn == addT) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("鑰佸笀ID锛�");
			slabel.setText("鑰佸笀濮撳悕锛�");
			tlabel.setText("瀛︾敓ID锛�");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == updateS) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("瑕佷慨鏀圭殑瀛︾敓ID锛�");
			slabel.setText("淇敼鍚庣殑鎬у悕锛�");
			tlabel.setText("淇敼鍚庣殑鑰佸笀ID锛�");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == updateT) {
			first.setEditable(true);
			second.setEditable(true);
			third.setEditable(true);
			flabel.setText("瑕佷慨鏀圭殑鑰佸笀ID锛�");
			slabel.setText("淇敼鍚庣殑鎬у悕锛�");
			tlabel.setText("淇敼鍚庣殑瀛︾敓ID锛�");
			operation = btn.getText();
			isStudent = false;
		} else if (btn == deleteS) {
			first.setEditable(true);
			flabel.setText("瑕佸垹闄ょ殑瀛︾敓ID锛�");
			slabel.setText("");
			tlabel.setText("");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == deleteT) {
			first.setEditable(true);
			flabel.setText("瑕佸垹闄ょ殑鑰佸笀ID锛�");
			slabel.setText("");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == choseIS) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("杩涜閫夋嫨鐨勮�佸笀ID锛�");
			slabel.setText("瑕侀�夋嫨鎸囧鐨勫鐢烮D锛�");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();

		} else if (btn == choseIT) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("杩涜閫夋嫨鐨勫鐢烮D锛�");
			slabel.setText("瑕侀�夋嫨鐨勮�佸笀ID锛�");
			tlabel.setText("");
			isStudent = true;
			operation = btn.getText();
		} else if (btn == updateIS) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("杩涜淇敼鐨勮�佸笀ID锛�");
			slabel.setText("淇敼鍚庢寚瀵肩殑瀛︾敓ID锛�");
			tlabel.setText("");
			isStudent = false;
			operation = btn.getText();
		} else if (btn == updateIT) {
			first.setEditable(true);
			second.setEditable(true);
			flabel.setText("杩涜淇敼鐨勫鐢烮D锛�");
			slabel.setText("淇敼鍚庣殑鎸囧鑰佸笀ID锛�");
			tlabel.setText("");
			operation = btn.getText();
			isStudent = true;
		} else if (btn == listAllS) {
			listStudent();
		} else if (btn == listAllT) {
			listTeacher();
		}
	}

	public Student studentOperation(String operation, Long sid) {
		StudentDAO studentDAO = new StudentDAO();
		// 寰楀埌瀛︾敓瀛﹀彿
		Long id = Long.parseLong(first.getText());
		// 寰楀埌瀛︾敓濮撳悕
		String name = second.getText();
		// 寰楀埌鑰佸笀宸ュ彿
		Long tid = (third.getText().length() > 0 ? Long.parseLong(third.getText()) : null);
		// 寰楀埌瀛︾敓瀹炰緥
		Student student = studentDAO.findById(id);
		// 寰楀埌鑰佸笀瀹炰緥
		Teacher teacher = teacherOperation("鏌ユ壘鑰佸笀", tid);
		// 寰楀埌瀛︾敓鍘熸潵鑰佸笀瀹炰緥
		Teacher oldT = (student == null ? null : student.getTeacher());
		switch (operation) {
		case "鏌ユ壘瀛︾敓":
			student = (sid == null ? null : studentDAO.findById(sid));
			break;
		case "娣诲姞瀛︾敓":
			student = new Student(id, name, teacher);
			studentDAO.save(student);
			break;
		case "淇敼瀛︾敓":
			student.setName(name);
			student.setTeacher(teacher);
			studentDAO.update(student, oldT);
			break;
		case "鍒犻櫎瀛︾敓":
			studentDAO.delete(student);
			break;
		case "閫夋嫨鎸囧鑰佸笀":
		case "淇敼鎸囧鑰佸笀":
			// 寰楀埌閫夋嫨鐨勬寚瀵艰�佸笀id
			tid = (second.getText().length() > 0 ? Long.parseLong(second.getText()) : null);
			teacher = teacherOperation("鏌ユ壘鑰佸笀", tid);
			student.setTeacher(teacher);
			studentDAO.update(student, oldT);
			break;
		default:
			break;
		}
		return student;
	}

	public Teacher teacherOperation(String operation, Long tid) {
		TeacherDAO teacherDAO = new TeacherDAO();
		StudentDAO studentDAO = new StudentDAO();
		// 鑰佸笀瀹炰緥
		Teacher teacher = null;
		// 寰楀埌鑰佸笀宸ュ彿
		Long id = Long.parseLong(first.getText());
		// 寰楀埌鑰佸笀濮撳悕
		String name = second.getText();
		// 寰楀埌寰呮寚瀵煎鐢熷鍙蜂覆
		String[] sidString = null;

		if (operation.equals("閫夋嫨寰呮寚瀵煎鐢�") || operation.equals("淇敼寰呮寚瀵煎鐢�"))
			sidString = (second.getText().length() > 0 ? second.getText().split(",|锛�") : new String[0]);
		else
			sidString = (third.getText().length() > 0 ? third.getText().split(",|锛�") : new String[0]);

		// 寰呮寚瀵煎鐢�
		Set<Student> students = new HashSet<Student>();
		for (int i = 0; i < sidString.length; i++) {
			Long tempID = Long.parseLong(sidString[i]);
			Student student = studentDAO.findById(tempID);
			students.add(student);
		}
		// 寰楀埌鑰佸笀瀹炰緥
		teacher = teacherDAO.findById(id);
		Set<Student> oldStudents = (teacher == null ? null : teacher.getStudents());
		switch (operation) {
		case "鏌ユ壘鑰佸笀":
			teacher = (tid == null ? null : teacherDAO.findById(tid));
			break;
		case "娣诲姞鏁欏笀":
			teacher = new Teacher(id, name);
			// 鍏堟柊寤鸿�佸笀鎵嶈兘鍒涘缓涓庡鐢熺殑鍏崇郴
			teacherDAO.save(teacher, students);
			break;
		case "淇敼鏁欏笀":
			teacher.setName(name);
			teacher.setStudents(students);
			teacherDAO.update(teacher, oldStudents);
			break;
		case "鍒犻櫎鏁欏笀":
			teacherDAO.delete(teacher);
			break;
		case "閫夋嫨寰呮寚瀵煎鐢�":
		case "淇敼寰呮寚瀵煎鐢�":
			teacher.setStudents(students);
			teacherDAO.update(teacher, oldStudents);
			break;
		default:
			break;
		}
		return teacher;
	}

	public void listStudent() {
		show.clear();
		StudentDAO studentDAO = new StudentDAO();
		List students = studentDAO.findAll();
		for (Object object : students) {
			Student student = (Student) object;
			if (student.getTeacher() != null)
				show.appendText("ID涓�" + student.getId() + "锛屽悕瀛椾负" + student.getName() + "鐨勫鐢燂紝鍏惰�佸笀ID涓猴細"
						+ student.getTeacher().getId() + "锛屽悕瀛椾负锛�" + student.getTeacher().getName());
			else
				show.appendText("ID涓�" + student.getId() + "锛屽悕瀛椾负" + student.getName() + "鐨勫鐢燂紝鏃犳寚瀵艰�佸笀");
			show.appendText("\n");
		}
	}

	public void listTeacher() {
		show.clear();
		TeacherDAO teacherDAO = new TeacherDAO();
		List teachers = teacherDAO.findAll();
		for (Object object : teachers) {
			Teacher teacher = (Teacher) object;
			show.appendText("ID涓�" + teacher.getId() + "锛屽悕瀛椾负" + teacher.getName() + "鐨勮�佸笀锛�");
			if (teacher.getStudents().size() > 0) {
				show.appendText("鎸囧鐨勫鐢熸湁锛�");
				for (Object student : teacher.getStudents()) {
					show.appendText(((Student) student).getId() + "-" + ((Student) student).getName() + " ; ");
				}
			} else {
				show.appendText("娌℃湁鎸囧瀛︾敓;");
			}
			show.appendText("\n");
		}
	}
}
